<template>
	<view class="container" >
		<view class="status_bar">
			<view style="margin-left: 3%;" @tap="goBack()"><u-icon name="arrow-leftward" size='24' /></view>
			<view style="margin-left: -50upx; font-weight: bold;">发朋友圈</view>
			<view></view>
			</u-icon>
		</view>


		<view class="shop-box">
			<view style="margin-bottom: 400upx;">
				<u--input v-model="value1" placeholder="请输入详细内容" border="none" clearable></u--input>
			</view>
			<view class="uplode-box">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:max-count="9" uploadIcon="plus" uploadIconColor="#bdbec0"></u-upload>
			</view>
		</view>
		<view class="con-shop">
			<view style="display: flex;">
				<view style="margin-top: 10upx;color: #313131;">展示范围</view>
			</view>
			<view style="display: flex;">
				<view style="margin-right: 20upx;">陌生人可见</view>
				<u-switch v-model="valueScope" @change="change" size=20 activeColor="#1ea7dd"
					inactiveColor="#ececec"></u-switch>
			</view>
		</view>
		<view class="btn-box" @click="add">发布</u-button></view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				startX: 0, // 触摸开始的X坐标
				endX: 0, // 触摸结束的X坐标
				threshold: 50, // 设置滑动多少距离后触发返回
				value1: '',
				type: '',
				fileList1: [],
				pictures: [],
				valueScope: true,
			}
		},
		onLoad(props) {
			console.log(props.type, "props")
			this.type = props.type

		},
		methods: {
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			
			
			change(e) {
				console.log('change', e)
				this.valueScope = e;
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					// this[`fileList${event.name}`].splice(fileListLen)
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					this.pictures.push(result)
					fileListLen++
				}


				console.log(this.pictures);
				console.log(this.fileList1);

			},
			uploadFilePromise(url) {
				console.log(url);
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$api.baseUrl + '/api/v1/common/upload',
						header: {
							'content-type': 'multipart/form-data'
						},
						filePath: url,
						name: 'file',
						data: {
							file: url,
						},
						success: (res) => {
							console.log(res.data);
							let img_item = JSON.parse(res.data);
							console.log(img_item);
							console.log(img_item.data);
							console.log(img_item.data.url);

							setTimeout(() => {
								resolve(img_item.data.url)
							}, 1000)
						},
						fail: res => {
							console.log(res);
						}
					});
					// return;
					// let a = uni.uploadFile({
					// 	url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
					// 	filePath: url,
					// 	name: 'file',
					// 	formData: {
					// 		user: 'test'
					// 	},
					// 	success: (res) => {
					// 		console.log(res);

					// 		setTimeout(() => {
					// 			resolve(res.data.data)
					// 		}, 1000)
					// 	}
					// });
				})
			},

			add() {
				if (this.value1 == "") {
					this.$util.toast('请输入内容')
				}
				console.log('add');
				console.log(this.value1);
				console.log(Number(this.valueScope));
				console.log(this.pictures);

				uni.request({
					// url: this.$api.baseUrl+'/penyouquan.json',
					url: this.$api.baseUrl + '/api/common/post/add',
					header: {
						'content-type': 'application/json'
					}, // this.$util.getHeader(),
					data: {
						post_type: 1,
						user_id: uni.getStorageSync('userId'),
						content: this.value1,
						is_private: Number(this.valueScope),
						pictures: JSON.stringify(this.pictures),
					},
					method: 'POST',
					success: res => {
						console.log(res);
						uni.hideLoading()

						if (res.data.code == 0) {
							this.$util.toast('发布成功')
							setTimeout(() => {
								uni.$emit('refreshData'); // 使用 uni.$emit 发送事件
								uni.navigateBack();
							}, 1500);
						}

					},
					fail: res => {
						console.log(res);
					}

				})
			}


		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100%;
	}

	.status_bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 50upx;
	}

	.status_bar,
	.head {
		background-color: #fff;
		height: 160upx;

		.face {
			width: 160upx;
			height: 160upx;
			border-radius: 10upx;
		}

		.info-name {
			font-size: 40upx;
			font-weight: bold;
		}

		.info-num {
			// width: 56upx;
			padding: 2upx 30upx;
			background: #f0f4f7;
			border-radius: 30upx;
			margin-top: 21upx;
		}

		.copybtns {
			color: #4457a4;
			margin-top: 21upx;
		}
	}


	.shop-box {
		width: 92%;
		height: auto;
		margin-left: 4%;
		background-color: #f8f9fb;
		margin-top: 20upx;
		border-radius: 20upx;
		padding: 10upx 30upx;
		color: #2e2e2e;
		font-weight: 300;
	}

	.con-shop {
		width: 92%;
		margin-left: 4%;
		height: 100upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.btn-box {
		width: 92%;
		margin-left: 4%;
		position: absolute;
		bottom: 80upx;
		background: #1a76f1;
		color: #fff;
		text-align: center;
		height: 80upx;
		line-height: 80upx;
		border-radius: 20upx;
	}
</style>